package cn.bdqn.controller;

import cn.bdqn.dao.CityDao;
import cn.bdqn.entity.City;
import cn.bdqn.entity.Dog;
import cn.bdqn.entity.PageVo;
import cn.bdqn.entity.Result;
import cn.bdqn.entity.vo.CityVO;
import cn.bdqn.service.CityService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.plugin.Interceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;

@RestController
@RequestMapping("city")
public class CityController {
    @Resource
    private CityService cityService;


    @Resource
    private CityDao cityDao;


    @GetMapping("bbb")
    public ResponseEntity<List<City>> queryAll1(Integer a,Integer b) {
        List<Integer> prams=null;
        if(a!=null && b!=null){
            prams=new ArrayList<>();
            for (int i=a;i<=b;i++){
                prams.add(i);
            }
        }
        return ResponseEntity.ok(cityDao.getByCountyCode(prams));
    }
    @GetMapping("ccc")
    public ResponseEntity<List<City>> queryAll2() {
        return ResponseEntity.ok(cityDao.likeName("ana"));
    }


    @GetMapping
    public Result<PageInfo<City>> queryAll(PageVo vo, CityVO cityVO) {
//        pageNum=pageNum==null?1:pageNum;
//        pageSize=pageSize==null?10:pageSize;
        //分页设置(当前第几页，一页多少条)
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        List<City> cities = this.cityService.queryAll(cityVO);
        //用PageInfo对象包装返回的结果
        PageInfo<City> pageInfo = new PageInfo<>(cities,10);
        return   Result.ok(pageInfo);
    }

    @GetMapping("{id}")
    public ResponseEntity<City> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.cityService.queryById(id));
    }

    @PostMapping
    public Result<City> add(@RequestBody City city) {
        return Result.ok(this.cityService.insert(city));
    }

    @PutMapping
    public ResponseEntity<City> edit(City city) {
        return ResponseEntity.ok(this.cityService.update(city));
    }
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.cityService.deleteById(id));
    }

}

